package com.jsu.springboot_crop.controller;

import com.github.pagehelper.PageInfo;
import com.jsu.springboot_crop.common.Result;
import com.jsu.springboot_crop.common.StatusCode;
import com.jsu.springboot_crop.entity.Discuss;
import com.jsu.springboot_crop.entity.Knowledge;
import com.jsu.springboot_crop.service.DiscussService;
import com.jsu.springboot_crop.service.KnowledgeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/knowledge")
public class KnowledgeController {
    @Autowired
    KnowledgeService knowledgeService;
    @Autowired
    DiscussService discussService;
    //查询所有农业知识
    @GetMapping("/{pageNum}")
    public Result<PageInfo<Knowledge>> findPage(@PathVariable("pageNum") Integer pageNum){
        PageInfo<Knowledge> knowledgePageInfo=knowledgeService.findPage(pageNum);
        return new Result<PageInfo<Knowledge>>(true, StatusCode.OK,"查询成功",knowledgePageInfo);
    }
    //条件查询农业知识
    @GetMapping("/{keys}/{pageNum}")
    public Result<PageInfo<Knowledge>> findPageByKeys(@PathVariable("keys") String keys, @PathVariable("pageNum") Integer pageNum){
        PageInfo<Knowledge> knowledgePageInfo=knowledgeService.findPageByKeys(keys,pageNum);
        return new Result<PageInfo<Knowledge>>(true, StatusCode.OK,"查询成功",knowledgePageInfo);
    }
    //根据id查询农业知识
    @GetMapping("/selectById/{id}")
    public Result<Knowledge> selectById(@PathVariable("id")Integer id){
        Knowledge knowledge = knowledgeService.selectById(id);
        return new Result<Knowledge>(true,StatusCode.OK,"查询成功",knowledge);
    }
    @GetMapping("/selectByUsername")
    public Result selectByUsername(){
        //在SerurityContexHolder中获取用户名，存入变量name中
        UserDetails principal=(UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String name=principal.getUsername();
//        String name="gaoge";
        List<Knowledge>knowledges=knowledgeService.selectByUsername(name);
        return new Result(true,StatusCode.OK,"查询成功",knowledges);
    }
    @PostMapping
    @PreAuthorize("hasAuthority('expert')")
    public Result add(@RequestBody Knowledge knowledge){
        knowledgeService.add(knowledge);
        return new Result(true,StatusCode.OK,"添加成功");
    }
    @PutMapping("/{id}")
    public Result update(@RequestBody Knowledge knowledge, @PathVariable("id")Integer id){
        knowledgeService.update(knowledge,id);
        return new Result(true,StatusCode.OK,"修改成功");
    }
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id")Integer id){
        knowledgeService.delete(id);
        return new Result(true,StatusCode.OK,"删除成功");
    }
    //评论
    //查询评论
    @GetMapping("/selectByKnowledge/{id}")
    public Result selectByKnowledge(@PathVariable("id")Integer id){
        List<Discuss>discuss= discussService.selectByKnowledgeId(id);
        return new Result(true,StatusCode.OK,"查询成功",discuss);
    }
    //增加评论
    @PostMapping("/addByKnowledge/{id}/{content}")
    public Result addByKnowledge(@PathVariable("id")Integer id, @PathVariable("content")String content){
        //获取用户名
//        //创建Discuss对象
        //获取登陆的用户名
        //获取登陆的用户名
        UserDetails principal = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String name = principal.getUsername();
        Discuss discuss=new Discuss();
        discuss.setKnowledgeId(id);
        discuss.setContent(content);
        discuss.setOwnName(name);
        //获取当前时间
//        impleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z");
//        Date date = new DSate(System.currentTimeMillis());
//        discuss.setCreateTime(formatter.format(date));
        discuss.setCreateTime(new Date());
        discussService.add(discuss);
        return new Result(true,StatusCode.OK,"添加成功",discuss);
        }
}
